package U2;

import java.io.*;
import java.util.stream.Collectors;
import java.util.*;

public class StudentManager {
    private static StudentManager instance =new StudentManager();
    private Map<String,Student> students=new HashMap();
    private Map<String, Student> getStudents() {
        return this.students;
    }
    private StudentManager(){}
    public static StudentManager getInstance(){
        return instance;
    }
    public void printAll(){    //打印学生信息
        Map<String,Student> ss=this.getStudents();
        for(Map.Entry<String,Student>entry:ss.entrySet()){
            Student student = entry.getValue();
            System.out.println(String.format("%s,%-6s,%s,%s,%s",student.getNum(),student.getName(),student.getChinese(),student.getMath(),student.getEnglish()));
        }
    }
    public Student getStudentByNum(String Num) {
        return this.students.get(Num);
    }
    public Student findByNum(String Num){
        return this.students.get(Num);
    }
    public void visit(IStudentVisitor visitor) {
        this.getStudents().forEach((key, val) -> {
            visitor.visit(val);
        });
    }
    public void addStudent(Student student) {
        this.students.put(student.getNum(), student);
    }
//根据课程类别，分值范围查询返回符合要求的学生列表;
    public List<Student> findByScore(ClassType classType, int min, int max) {
        List<Student> result = new ArrayList<>();
        this.getStudents().forEach((num, student) -> {
            int score;
            switch (classType) {
                case MATH : {score = student.getMath(); break;}
                case CHINESE : {score = student.getChinese(); break;}
                case ENGLISH : {score = student.getEnglish(); break;}
                default : throw new IllegalArgumentException("错误的学科类别");
            }

            if (score >= min && score <= max) {
                result.add(student);
            }
        });
        return result;
    }

    public List<Student> findByAverage(int min, int max) {   //查找某平均分区间学生信息
        List<Student> result = new ArrayList<>();
        this.getStudents().forEach((id, student) -> {
            if ( student.getAverage() >= min  && student.getAverage() <= max) {
                result.add(student);
            }
        });
        return result;
    }

    public List<Student> sortByTotal() {      //降序
        return this.sortByTotal(SortType.DESC);
    }

//返回一个列表，列表中的学生按总分由高到低排序
    public List<Student> sortByTotal(SortType sortType) {
        List<Student> students = this.getStudents().values().stream().collect(Collectors.toList());
        students.sort(new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                if (s1.getTotal() > s2.getTotal()) {
                    return sortType.equals(SortType.ASC) ? 1 : -1;
                } else if (s1.getTotal() < s2.getTotal()) {
                    return sortType.equals(SortType.ASC) ? -1 : 1;
                } else {
                    return 0;
                }
            }
        });
        return students;
    }

    //将学生信息按字段存到文件中，并用逗号隔开
    public void saveAs(String filePath) throws IOException {
        Map<String, Student> ss = this.getStudents();
        FileWriter writer = new FileWriter(filePath);
        for (Iterator<String> it = ss.keySet().iterator(); it.hasNext(); ) {
            String key = it.next();
            Student student = ss.get(key);
            writer.write(String.format(
                    "%s, %s, %s, %s, %s",
                    student.getNum(), student.getName(), student.getChinese(), student.getMath(), student.getEnglish()
            ));
        }
        writer.close();
    }
}
